// 定义白名单  对路由进行拦截
const whitePath = ['/login','/registry','/verify'];
const jwt = require("jsonwebtoken");
module.exports = () => {
    return async (ctx, next) => {
        console.log('--------------', ctx.path);
        if (whitePath.includes(ctx.path)) {
            await next();
        } else {
            let token = ctx.request.headers.authorization;
            console.log(token,"55555555555555");
            // 判断是否有取到 token
            if (!token) {
                ctx.body = {
                    code: 401,
                    msg: "没有权限访问"
                }
                ctx.status = 401;
                return;
            }
            try {
                // 校验 token 是否正确
                 jwt.verify(token, '123');
                // ctx.userInfo = userInfo;
                await next();
            } catch (error) {
                console.log('错误拦截',error)
                ctx.body = {
                    code: 500,
                    msg: "校验失败"
                }
               
            }

        }
    }
}